<?php
require_once('wpfunctions.php');
header('Content-type: text/javascript');
$add=$_REQUEST['add'];
$lang=$_REQUEST['lang'];
$dicFiles = ip_allFiles("dictionaries/$lang.txt");
foreach ($dicFiles as $file) {
  if(file_exists($file)) {
    $dicFile = $file;
    break;
  }
}
if(!$dicFile) {
  return;
}
$dic = file_get_contents($dicFile);
$dicArray = explode("\n", $dic);
if(!is_array($add)) {
  if($add[0]=='[') {
    $add = json_decode($add);
  } else {
    $add = explode(' ',$add);
  }
}
if(is_array($add)) {
  foreach($add as $key => $val) {
    if(strlen($val) > 0) {
      $dicArray[] = $val;
    }
  }
  $dicArray = array_unique($dicArray);
  sort($dicArray);
  $dic = implode("\n",$dicArray);
  file_put_contents($dicFile,$dic);
}
$dic = implode(' ',$dicArray);
echo "Dic.addEntries('$lang','$dic');";
?>